﻿Shader "Custom/TestShader19"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
        _BlurSize("BlurSize",float) = 1
        _Bloom("Bloom", 2D) = "white" {}
        _LuminanceThreshold ("LuminanceThreshold", float) = 0.5
    }

    SubShader
    {
        CGINCLUDE
        #include "UnityCG.cginc"

        sampler2D _MainTex;
        float4 _MainTex_ST;
        float4 _MainTex_TexelSize;
        float _BlurSize;
        sampler2D _Bloom;
        float _LuminanceThreshold;

        struct v2f
        {
            float4 pos : SV_POSITION;
            float2 uv : TEXCOORD0;
        };

        fixed luminance(fixed4 color)
        {
            fixed3 gray = fixed3(0.2125, 0.7154, 0.0721);
            return dot(gray, color.rgb);
        }

        v2f vertExtractBright(appdata_img v)
        {
            v2f o;
            o.pos = UnityObjectToClipPos(v.vertex);
            o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
            return o;
        }
        
        fixed4 fragExtractBright(v2f i) : SV_Target
        {
            fixed4 c = tex2D(_MainTex, i.uv);
            fixed bright = luminance(c);
            fixed val = clamp(bright - _LuminanceThreshold, 0, 1);
            return c * val;
        }

        struct v2fBloom
        {
            float4 pos : SV_POSITION;
            float4 uv : TEXCOORD0;
        };

        v2fBloom vertBloom(appdata_img v)
        {
            v2fBloom o;
            o.pos = UnityObjectToClipPos(v.vertex);
            o.uv.xy = v.texcoord;
            o.uv.zw = v.texcoord;
#if UNITY_UV_STARTS_AT_TOP
            if (_MainTex_TexelSize.y < 0)
            {
                o.uv.w = 1 - o.uv.w;
            }
#endif
            return o;
        }

        fixed4 fragBloom(v2fBloom i) : SV_Target
        {
            return tex2D(_MainTex, i.uv.xy) + tex2D(_Bloom, i.uv.zw);
        }

        ENDCG
        ZWrite Off
        Cull Off
        ZTest Always

        Pass
        {
            CGPROGRAM
            #pragma vertex vertExtractBright
            #pragma fragment fragExtractBright
            ENDCG
        }
        UsePass "Custom/TestShader18/GAUSSIAN_BLUR_VERTICAL"

        UsePass "Custom/TestShader18/GAUSSIAN_BLUR_HORIZONTAL"

        Pass
        {
            CGPROGRAM
            #pragma vertex vertBloom
            #pragma fragment fragBloom
            ENDCG
        }
	}
}
